package cn.coldleaf.wiki.controllers;

import cn.coldleaf.wiki.annotations.IgnoreAop;
import cn.coldleaf.wiki.pojo.dto.FileUploadDto;
import cn.coldleaf.wiki.pojo.vo.ResponseVo;
import cn.coldleaf.wiki.pojo.vo.UploadVo;
import cn.coldleaf.wiki.services.StreamService;
import cn.coldleaf.wiki.utils.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 流Controller
 * 返回文件流
 * @datetime 2023/10/17 下午2:55
 * @user cold
 **/
@Controller
@RequestMapping("/")
@AllArgsConstructor
@Api(tags = "文件操作接口")
public class StreamController {
    private final StreamService streamService;
    /**
     * 图片流
     * @datetime 2023-10-17 14:57:42
     * @user cold
     **/
    @GetMapping("/file/{type}/{name}")
    public void file(HttpServletResponse response,@PathVariable String type, @PathVariable String name,Long operator){
        streamService.file(response,operator,type,name);
    }
    /**
     * 验证码图片
     * @datetime 2023-10-17 14:57:42
     * @user cold
     **/
    @IgnoreAop
    @GetMapping("captcha")
    @Operation(summary = "获取图形验证码")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        streamService.captcha(response);
    }

    /**
     * 下载文件
     * @datetime 2023-10-17 14:57:42
     * @user cold
     **/
    public void download(HttpServletRequest request, HttpServletResponse response){
    }

    /**
     * 上传文件
     * @datetime 2023-10-19 16:48:50
     * @user cold
     **/
    @ResponseBody
    @PostMapping(value = "file/upload")
    @Operation(summary = "上传文件")
    public ResponseVo<UploadVo> upload(FileUploadDto dto){
        UploadVo vo = streamService.upload(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.FILE_UPLOAD_ERROR);
    }
}
